package com.stiff.manage.controller.api;

import com.stiff.common.bean.ItemCatResult;
import com.stiff.manage.service.ItemCatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("api/item/cat")
public class ApiItemCatController{
    private static final Logger LOGGER = LoggerFactory.getLogger(ApiItemCatController.class);
    @Autowired
    private ItemCatService itemCatService;

//    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * 对外接口查询所有商品类目树结构数据
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<ItemCatResult> queryItemCat(){
        try {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("【查询所有商品类目树结构】");
            }
            ItemCatResult itemCatResult = this.itemCatService.queryAllToTree();
            if(null==itemCatResult){
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }
            return ResponseEntity.ok(itemCatResult);
        } catch (Exception e) {
            LOGGER.error("【查询所有商品类目树结构失败！】",e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }
//    /**
//     * 对外接口查询所有商品类目树结构数据
//     * @return
//     */
//    @RequestMapping(method = RequestMethod.GET)
//    public ResponseEntity<String> queryItemCat(@RequestParam("callback")String callback){
//        try {
//            if (LOGGER.isInfoEnabled()) {
//                LOGGER.info("【查询所有商品类目树结构】");
//            }
//            ItemCatResult itemCatResult = this.itemCatService.queryAllToTree();
//            if(null==itemCatResult){
//                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
//            }
//            String json = MAPPER.writeValueAsString(itemCatResult);
//            if(StringUtils.isEmpty(callback)){
//                return ResponseEntity.ok(json);
//            }
//            return ResponseEntity.ok(callback+"("+json+");");
//        } catch (Exception e) {
//            LOGGER.error("【查询所有商品类目树结构失败！】",e);
//        }
//        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
//    }

}
